home *** CD-ROM | disk | FTP | other *** search
/ Delphi Programmer's Power Pack / Delphi Volume 1.iso / e_to_l / fbuilder / delphi / demos / varxmpfm.pas < prev    next >
Pascal/Delphi Source File  |  1996-09-15  |  7KB  |  234 lines

  1. { FormulaBuilder                }
  2. { YGB Software, Inc.            }
  3. { Copyright 1995 Clayton Collie }
  4. { All rights reserved           }
  5.  
  6. {* Simple demonstration of the different variable access methods of *}
  7. {* a TExpression                                                    *}
  8. {* Also demonstrates registering a new function with the engine     *}
  9. {*                                                                  *}
  10. unit Varxmpfm;
  11. interface
  12. uses
  13.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  14.   Forms, Dialogs,
  15.   FBCalc,FBComp,
  16.   Buttons, StdCtrls;
  17.  
  18. type
  19.   TVarform = class(TForm)
  20.     ResultMemo: TMemo;
  21.     Button1: TButton;
  22.     Button2: TButton;
  23.     BitBtn1: TBitBtn;
  24.     Label1: TLabel;
  25.     Button3: TButton;
  26.     Button4: TButton;
  27.     procedure FormCreate(Sender: TObject);
  28.     procedure FormDestroy(Sender: TObject);
  29.     procedure Button1Click(Sender: TObject);
  30.     procedure Button2Click(Sender: TObject);
  31.     procedure Button3Click(Sender: TObject);
  32.     procedure Button4Click(Sender: TObject);
  33.   private
  34.     { Private declarations }
  35.   public
  36.     { Public declarations }
  37.     SimpleExpression : TExpression;
  38.     Procedure TryVariables;
  39.     Procedure TryVarptr;
  40.     Procedure TryVariableList;
  41.     Procedure TryStringValues;
  42.     Procedure UpdateCalc(const i : integer;const x, y : longint);
  43.   end;
  44.  
  45. var
  46.   Varform: TVarform;
  47.  
  48. implementation
  49.  
  50. {$R *.DFM}
  51. var myFuncId : integer;
  52.  
  53. {Custom callback proc
  54.  SYNTAX  : MYFUNC(X, Y)
  55.  RETURNS : X + Y
  56. }
  57. Procedure MyProc( nParamcount     : byte;
  58.                   const params  : TActParamList;
  59.                   var retvalue  : TVALUEREC;
  60.                   var nErrCode  : Integer;
  61.                   ExprData      : longint); export;
  62. var
  63.     intval1     : longint;
  64.     intVal2     : longint;
  65. begin
  66.   intval1 := params[0].vInteger;
  67.   intVal2 := params[1].vInteger;
  68.   retvalue.vInteger := intval1 + intval2;
  69.   nErrcode := EXPR_SUCCESS;
  70. end;
  71.  
  72.  
  73. Procedure TVarform.UpdateCalc(const i : integer;const x, y : longint);
  74. var tmp : string[50];
  75. begin
  76.  { Evaluate the expression and place results in memo }
  77.  tmp := IntToStr(i)+ '. X = '+IntToStr(x) +
  78.         ', Y = '+IntToStr(y)+', Result = ' + SimpleExpression.AsString;
  79.  ResultMemo.Lines.Add(tmp);
  80. end;
  81.  
  82.  
  83. { Use the Variables array property }
  84. Procedure TVarform.TryVariables;
  85. var i   : integer;
  86.     x,y : TValueRec;
  87.     tmp : string;
  88.  
  89. begin
  90.   ResultMemo.Lines.Add('Method 1 : Property access via Variables Property');
  91.   x.vtype := vtInteger; { this is necessary. Note that even        }
  92.   y.vtype := vtInteger; { if x and y were defined as int variables }
  93.                         { Integers and Floats are type compatible, so }
  94.                         { we could set the value of an integer variable to }
  95.                         { a float, and vice versa }
  96.   with simpleExpression do
  97.   begin
  98.     for i := 1 to 10 do
  99.     begin
  100.       x.vInteger := (Random(10) + 1) * i;
  101.       y.vInteger := (Random(5)  + 1) * i;
  102.       variables['X']  := x;
  103.       variables['Y']  := y;
  104.       UpdateCalc(i,x.vInteger,y.vInteger);
  105.     end;
  106.   end;
  107. end;
  108.  
  109.  
  110. { Access variables using the VariableList property }
  111. Procedure TVarform.TryVariableList;
  112. var i   : integer;
  113.     x,y : TVariable;
  114.     tmp : string;
  115.  
  116. begin
  117.   ResultMemo.Lines.Add('Method 3 : Access via VariableList Property');
  118.   with simpleExpression do
  119.   begin
  120.     x := variableList[0]; { we know x was the first to be added }
  121.     y := variableList[1]; { and y was the second }
  122.     { we had to read the values first so that the  }
  123.     { vtype field of the Value structure would be set }
  124.  
  125.     for i := 1 to 10 do
  126.     begin
  127.       x.value.vInteger := (Random(10) + 1) * i;
  128.       y.value.vInteger := (Random(5)  + 1) * i;
  129.       variableList[0]  := x;
  130.       variableList[1]  := y;
  131.       UpdateCalc(i,x.value.vInteger,y.value.vInteger);
  132.     end;
  133.   end;
  134. end;
  135.  
  136.  
  137. { Access variables using the StringValues property }
  138. Procedure TVarform.TryStringValues;
  139. var i   : integer;
  140.     xstr, ystr : string[10];
  141.  
  142. begin
  143.   ResultMemo.Lines.Add('Method 4 : Access via StringValues Property');
  144.   with simpleExpression do
  145.   begin
  146.     for i := 1 to 10 do
  147.     begin
  148.       xstr := intTostr( (Random(10) + 1) * i );
  149.       ystr := inttostr( (Random(5)  + 1) * i );
  150.       StringValues['X'] := xstr;
  151.       StringValues['Y'] := ystr;
  152.       UpdateCalc(i,strToInt(xstr),strToInt(ystr));
  153.     end;
  154.   end;
  155. end;
  156.  
  157.  
  158.  
  159. { Use this method when you need greater speed. This method allows  }
  160. { direct access to the variable data maintained internally by formulaBuilder }
  161. { be very cautious not to alter string pointers obtained through GetVarPtr }
  162. { i will be looking at an easy way of allowing you to work with strings }
  163. { using this method. }
  164. Procedure TVarform.TryVarPtr;
  165. var i   : integer;
  166.     x,y   : ^longint;
  167.     vtype : byte;
  168.     tmp   : string;
  169.     res   : integer;
  170.  
  171. begin
  172.   ResultMemo.Lines.Add('Method 2 : Direct access to variable data ');
  173.   ResultMemo.Lines.Add('Formula  : '+SimpleExpression.Formula);
  174.   with simpleExpression do
  175.   begin
  176.     GetVarPtr('X',vtype,pointer(x));  { obtain a pointer to the variable data }
  177.     GetVarPtr('Y',vtype,pointer(y));  { vtype returns the type of variable }
  178.     for i := 1 to 10 do
  179.     begin
  180.       x^ := (Random(10)+ 1) * i;
  181.       y^ := (Random(25)+ 1) * i;
  182.       UpdateCalc(i,x^,y^);
  183.     end;
  184.   end;
  185. end;
  186.  
  187.  
  188. procedure TVarform.FormCreate(Sender: TObject);
  189. var s: String;
  190. begin
  191.    SimpleExpression := TExpression.Create(NIL);
  192.    MyFuncID := FBRegisterFunction ('MyFunc',vtInteger,'ii',2,MyProc);
  193.    MessageDlg ('MyFuncID is: ' + IntToStr(MyFuncID),mtinformation,[mbok],0);
  194.    s := 'Myfunc(x,y)';
  195.    with simpleExpression do
  196.    begin
  197.     { NOTE! variables must be added before the formula property is set to }
  198.     { an expression involving them. Otherwise, we get an Invalid Identifier error}
  199.  
  200.      AddVariable ('X',vtInteger);
  201.      AddVariable ('Y',vtInteger);
  202.      Formula := S;
  203.      Randomize;
  204.    end; {with}
  205. end;
  206.  
  207. procedure TVarform.FormDestroy(Sender: TObject);
  208. begin
  209.   FBUnregisterFunction(myFuncId);
  210.   SimpleExpression.Free;
  211. end;
  212.  
  213. procedure TVarform.Button1Click(Sender: TObject);
  214. begin
  215.   TryVariables;
  216. end;
  217.  
  218. procedure TVarform.Button2Click(Sender: TObject);
  219. begin
  220.  TryVarPtr;
  221. end;
  222.  
  223. procedure TVarform.Button3Click(Sender: TObject);
  224. begin
  225.    TryVariableList;
  226. end;
  227.  
  228. procedure TVarform.Button4Click(Sender: TObject);
  229. begin
  230.   TryStringValues;
  231. end;
  232.  
  233. end.
  234.